<?php
declare (strict_types = 1);

namespace app\article1\controller;

use app\article1\model\User;
use app\BaseController;
use app\services\SmsServices;
use think\facade\Cache;
use think\Request;

class login extends BaseController
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function login()
    {
        return view('login');
    }
    public function register(Request $request){
        if($request->post()){
            $params=$request->post();
//            dd($params);
            try {
                $this->validate($params,[
                   'phone'=>'require|mobile|unique:user',
                    'code'=>'require',
                    'password'=>'require',
                    'repassword'=>'require|confirm:password'
                ],[
                    'repassword.confirm'=>'俩次密码不一致',
                    'phone.unique'=>'手机号已经注册，请不要重复注册'
                ]);

                //校验验证码是否正确
                $smsCodeKey='sms_code_'.$params['phone'];
                $smsSendTimeKey='sms_code_time'.$params['phone'];
                $cacheCode=Cache::get($smsCodeKey);

                //检测是否存在
                if(!$cacheCode){
                    abort(101,'请发送验证码');
                }

                //验证码是否一致
                if($cacheCode !=$params['code']){
                    abort(102,'请输入正确的验证码');
                }
                $data=[
                   'phone'=>$params['phone'],
                   'username'=>$params['phone'],
                   'password'=>md5($params['password']),
                ];
                $user=User::create($data);
                if($user->id){
                    //清理工作
                    Cache::delete($smsCodeKey);
                    //清理发送时间
                    Cache::delete($smsSendTimeKey);
                    return ['status'=>1,'msg'=>'注册成功，请去登录'];
                }else{
                    return ['status'=>0,'msg'=>'注册失败'];
                }

            }catch (\Exception $e){
                return ['status'=>0,'msg'=>$e->getMessage()];
            }
        }
    }
    public function sendSms(Request $request){
        $params=$request->post();
        try {
            $code=mt_rand(1000,9999);
            $content='我是测试码:'.$code;

            //封装
            $smsSendTimeKey='sms_code_time'.$params['phone'];
            $smsCodeKey='sms_code_'.$params['phone'];
            $smsSendDayKey='sms_day_'.$params['phone'].'_'.date('ymd');

            //发送次数限制
            $sendDayTimes=Cache::get($smsSendDayKey);
            if($sendDayTimes && $sendDayTimes>5){
                abort(103,'验证码超出一天发送的次数');
            }

            //发送频率限制
            $sendTime=Cache::get($smsSendTimeKey);
            if($sendTime){
                if(time()-$sendTime <30){
                    $endTime=30-(time()-$sendTime);
                    abort(102,'你的发送频率过于频繁，请'.$endTime.'秒后再试');
                }
            }


            $res=SmsServices::send($params['phone'],$content, true);

            if($res['status']==1){
                //短信缓存，10分钟失效
                Cache::set($smsCodeKey,$code,600);
                //第一次发送时间
                Cache::set($smsSendTimeKey,time(),30);
                //自增
                Cache::inc($smsSendDayKey);
                $res['code']=$code;
                return $res;
            }else{
                abort(101,$res['msg']);
            }
        } catch (\Exception $e){
            return ['status'=>0,'msg'=>$e->getMessage()];
        }

    }
}
